<?php
require 'db.php';

if(isset($_POST['name']) && !empty($_POST['name'])){
  $name = $_POST['name'];
  $insert = "INSERT INTO `countries`
     (`name`)VALUES('$name');";

mysql_query($insert) or die(mysql_error());

$rowsInserted =  mysql_affected_rows();
$lastId = mysql_insert_id();

echo "<p>$rowsInserted country added on Id:$lastId<p>";
}
//////////////////////////////////////////////////
$select = "SELECT * FROM `countries` ORDER BY `name`";

$result = mysql_query($select) or die(mysql_error());
$rowsSelected =  mysql_num_rows($result);
echo "<h4>$rowsSelected Countries Selected</h4>";
//////////////////////////////////////////////////
?>
<form action="" method="post" >
  <table width="40%" align="center" border="1">
    <thead>
      <tr>
        <th colspan="2">Add Country</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Name</td>
        <td><input type="text" name="name"></td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td><input type="submit" value="Add me!!" /></td>
      </tr>
    </tbody>
  </table>
</form>

  <table width="40%" align="center" border="1">
  <thead>
    <tr>
      <th>#</th>
      <th>Country</th>
    </tr>
  </thead>
  <tbody>
<?php
$i=1;
while($ans = mysql_fetch_assoc($result)){
?>
    <tr>
      <td><?=$i++?></td>
      <td><?=$ans['name']?></td>
    </tr>
<?php
}//end while
?>
  </tbody>
</table>
